/*
 * @lc app=leetcode.cn id=859 lang=javascript
 *
 * [859] 亲密字符串
 */

// @lc code=start
/**
 * @param {string} A
 * @param {string} B
 * @return {boolean}
 */
var buddyStrings = function(A, B) {
    let sa="",sb=""
    let flag=true
    let max=0
    let map=new Map()
    if(A.length!=B.length||A.length==0||B.length==0)return false
    for(let i=0;i<A.length;i++){
        if(A[i]!==B[i]){
            flag=false
            sa+=A[i]
            sb+=B[i]
            if(sa.length>2)return false
        }
        if(flag){
            if(map.has(A[i])){
                map.set(A[i],map.get(A[i])+1)
                max=Math.max(map.get(A[i]),max)
            }else{
                map.set(A[i],1)
            }
        }
    }
    if(!flag&&sa.length!=2)return false
    if(flag){
        return max>=2
    }
    return sa[0]==sb[1]&&sa[1]==sb[0]
};
// @lc code=end

